package com.yyfax.ph.personal.authority.bankcard.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.widget.FrameLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.R;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.base.dialog.DialogBase;
import com.yyfax.ph.common.utils.Constants;
import com.yyfax.ph.personal.authority.bankcard.model.UnbindCardModel;
import com.yyfax.ph.personal.authority.bankcard.presenter.UnbindCardPresenter;

/**
 * 解绑银行卡界面
 * Created by zhangxian on 2017/11/4.<br>
 * Copyright 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class GetBindCardActivity extends BaseYYFaxActivity implements UnbindCardPresenter.View {
	public static final String CARD_ID = "mCardId";
	public static final String NAME = "mName";
	public static final String BANK_NAME = "mBankName";
	public static final String BANK_CODE = "mBankCode";
	private static int LOADER_ID = 1;

	@BindView(R.id.bank_card_fl)
	FrameLayout mBankCardFl;
	@BindView(R.id.bank_card_tv)
	TextView mBankCardTv;

	private String mCardId;
	private String mName;
	private String mBankName;
	private String mBankCode;
	private UnbindCardPresenter mPresenter;
	private DialogBase mDialog;

	public static void start(Activity aty, String cardId, String name, String bank, String bankCode) {
		Intent intent = new Intent(aty, GetBindCardActivity.class);
		intent.putExtra(GetBindCardActivity.CARD_ID, cardId);
		intent.putExtra(GetBindCardActivity.NAME, name);
		intent.putExtra(GetBindCardActivity.BANK_NAME, bank);
		intent.putExtra(GetBindCardActivity.BANK_CODE, bankCode);
		aty.startActivity(intent);
	}

	@Override
	public int getContentViewRes() {
		return R.layout.get_bind_card_activity_layout;
	}

	@Override
	public void initContentView() {
		setBtnRightTx(R.string.unbind_str);
		setTitleLeftVisible(false);
		setTitleVisible(true);
		setTitle(R.string.bank_card_manage);
		if (!TextUtils.isEmpty(mBankCode)) {
			bindView();
		}
	}

	//todo 显示对应的银行卡
	void bindView() {
		int imgResId = -1;
		switch (mBankCode) {
			case Constants.BANK_CODE_ICBC:
				imgResId = R.drawable.bg_bank_icbc;
				break;
			case Constants.BANK_CODE_ABC:
				imgResId = R.drawable.bg_bank_abc;
				break;
			case Constants.BANK_CODE_BOC:
				imgResId = R.drawable.bg_bank_boc;
				break;
			case Constants.BANK_CODE_CCB:
				imgResId = R.drawable.bg_bank_ccb;
				break;
			case Constants.BANK_CODE_CITIC:
				imgResId = R.drawable.bg_bank_citic;
				break;
			case Constants.BANK_CODE_CEB:
				imgResId = R.drawable.bg_bank_ceb;
				break;
			case Constants.BANK_CODE_HXB:
				imgResId = R.drawable.bg_bank_hxb;
				break;
			case Constants.BANK_CODE_GDB:
				imgResId = R.drawable.bg_bank_gdb;
				break;
			case Constants.BANK_CODE_PINGAN:
				imgResId = R.drawable.bg_bank_pingan;
				break;
			case Constants.BANK_CODE_CIB:
				imgResId = R.drawable.bg_bank_cib;
				break;
			case Constants.BANK_CODE_CMB:
				imgResId = R.drawable.bg_bank_cmb;
				break;
			case Constants.BANK_CODE_SPDB:
				imgResId = R.drawable.bg_bank_spdb;
				break;
			case Constants.BANK_CODE_PSBC:
				imgResId = R.drawable.bg_bank_psbc;
				break;
			case Constants.BANK_CODE_CMBC:
				imgResId = R.drawable.bg_bank_cmbc;
				break;
			case Constants.BANK_CODE_BEIJING:
				imgResId = R.drawable.bg_bank_beijing;
				break;
			default:
				break;
		}
		if (imgResId != -1) {
			mBankCardFl.setBackgroundResource(imgResId);
		}
		if (!TextUtils.isEmpty(mCardId)) {
			int length = mCardId.length();
			if (length > 4) {
				mBankCardTv.setText(mCardId.substring(length - 4, length));
			} else {
				mBankCardTv.setText(mCardId);
			}
		}
	}

	@Override
	public void pretreatmentAfterSetBaseView() {
		super.pretreatmentAfterSetBaseView();
		if (null == getIntent())
			return;
		if (getIntent().hasExtra(GetBindCardActivity.CARD_ID))
			mCardId = getIntent().getStringExtra(GetBindCardActivity.CARD_ID);
		if (getIntent().hasExtra(GetBindCardActivity.NAME))
			mName = getIntent().getStringExtra(GetBindCardActivity.NAME);
		if (getIntent().hasExtra(GetBindCardActivity.BANK_NAME))
			mBankName = getIntent().getStringExtra(GetBindCardActivity.BANK_NAME);
		if (getIntent().hasExtra(GetBindCardActivity.BANK_CODE))
			mBankCode = getIntent().getStringExtra(GetBindCardActivity.BANK_CODE);
	}

	@Override
	public void onBackBtnPressed() {
		super.onBackBtnPressed();
		finish();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	@Override
	public void unbindCardSuccess(UnbindCardModel.UnbindCardBean bean) {
		showToast(R.string.unbind_success_toast);
		Intent intent = new Intent(this, BindCardActivity.class);
		intent.putExtra(GetBindCardActivity.NAME, bean.name);
		intent.putExtra(GetBindCardActivity.CARD_ID, bean.certId);
		BindCardActivity.start(this, intent);
		finish();
	}

	@Override
	public void unbindCardFailed(String tips) {
		showTipsDialog(tips);
	}

	@Override
	public void checkUnbindSuccess(String tips) {
		showTipsDialog(getString(R.string.unbind_bank_card_tips), R.string.cancel, R.string.unbind_str, new DialogBase.BottomTwoClickListener() {
			@Override
			public void onCancelClick(DialogBase dialog) {
				dialog.dismiss();
			}

			@Override
			public void onConfirmClick(DialogBase dialog) {
				dialog.dismiss();
				mPresenter.unbindCard();
			}
		}, false);
	}

	@Override
	public void checkUnbindFailed(String tips) {
		showTipsDialog(tips);
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		mPresenter = (UnbindCardPresenter) data;
		mPresenter.attach(this);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new UnbindCardPresenter.Generator());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ButterKnife.bind(this);
		getSupportLoaderManager().initLoader(LOADER_ID, null, this);
	}

	@Override
	public void onRightBtnPressed() {
		StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_E030);
		mPresenter.checkUnbind();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		getSupportLoaderManager().destroyLoader(LOADER_ID);
	}
}
